using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class Dictionary
	{
		private static Dictionary _instance = null;
		public static Dictionary instance
		{
			get
			{
				if (Dictionary._instance == null)
				{
					Dictionary._instance = new Dictionary();
				}
				return Dictionary._instance;
			}
		}
		public DataSet GetDataByTable_Field(string TableName, string FieldName)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT Title,FieldText,FieldValue FROM HL_Dictionary WHERE TableName=@TableName AND FieldName=@FieldName order by FieldID ASC");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@TableName", SqlDbType.VarChar, 30),
				new SqlParameter("@FieldName", SqlDbType.VarChar, 30)
			};
			array[0].Value = TableName;
			array[1].Value = FieldName;
			return Conn.Query(stringBuilder.ToString(), array);
		}
		public DataSet GetDataByParentValue(string ParentValue, string where)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			if (where.Trim() != "")
			{
				where = string.Format(" and {0} ", where);
			}
			stringBuilder.Append(string.Format("SELECT Title,FieldText,FieldValue,DictType FROM HL_Dictionary WHERE ParentValue=@ParentValue {0} order by FieldID ASC", where));
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ParentValue", SqlDbType.VarChar, 30)
			};
			array[0].Value = ParentValue;
			return Conn.Query(stringBuilder.ToString(), array);
		}
		public string GetTextByValue(string FieldValue)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT TOP 1 FieldText FROM HL_Dictionary WHERE FieldValue=@FieldValue ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FieldValue", SqlDbType.NVarChar, 100)
			};
			array[0].Value = FieldValue;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			return (single == null) ? "" : single.ToString();
		}
		public string GetValueByText(string FieldText)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT TOP 1 FieldValue FROM HL_Dictionary WHERE FieldText=@FieldText ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FieldText", SqlDbType.NVarChar, 100)
			};
			array[0].Value = FieldText;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			return (single == null) ? "" : single.ToString();
		}
		public int GetMaxId()
		{
			return Conn.GetMaxID("FieldID", "HL_Dictionary");
		}
		public bool Exists(int FieldID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_Dictionary");
			stringBuilder.Append(" where FieldID=@FieldID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FieldID", SqlDbType.Int, 4)
			};
			array[0].Value = FieldID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.Dictionary model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_Dictionary(");
			stringBuilder.Append("Title,TableName,FieldName,FieldText,FieldValue)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@Title,@TableName,@FieldName,@FieldText,@FieldValue)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Title", SqlDbType.NVarChar, 50),
				new SqlParameter("@TableName", SqlDbType.NVarChar, 30),
				new SqlParameter("@FieldName", SqlDbType.NVarChar, 30),
				new SqlParameter("@FieldText", SqlDbType.NVarChar, 100),
				new SqlParameter("@FieldValue", SqlDbType.NVarChar, 100)
			};
			array[0].Value = model.Title;
			array[1].Value = model.TableName;
			array[2].Value = model.FieldName;
			array[3].Value = model.FieldText;
			array[4].Value = model.FieldValue;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.Dictionary model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_Dictionary set ");
			stringBuilder.Append("Title=@Title,");
			stringBuilder.Append("TableName=@TableName,");
			stringBuilder.Append("FieldName=@FieldName,");
			stringBuilder.Append("FieldText=@FieldText,");
			stringBuilder.Append("FieldValue=@FieldValue");
			stringBuilder.Append(" where FieldID=@FieldID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Title", SqlDbType.NVarChar, 50),
				new SqlParameter("@TableName", SqlDbType.NVarChar, 30),
				new SqlParameter("@FieldName", SqlDbType.NVarChar, 30),
				new SqlParameter("@FieldText", SqlDbType.NVarChar, 100),
				new SqlParameter("@FieldValue", SqlDbType.NVarChar, 100),
				new SqlParameter("@FieldID", SqlDbType.Int, 4)
			};
			array[0].Value = model.Title;
			array[1].Value = model.TableName;
			array[2].Value = model.FieldName;
			array[3].Value = model.FieldText;
			array[4].Value = model.FieldValue;
			array[5].Value = model.FieldID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int FieldID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_Dictionary ");
			stringBuilder.Append(" where FieldID=@FieldID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FieldID", SqlDbType.Int, 4)
			};
			array[0].Value = FieldID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string FieldIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_Dictionary ");
			stringBuilder.Append(" where FieldID in (" + FieldIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.Dictionary GetModel(int FieldID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 FieldID,Title,TableName,FieldName,FieldText,FieldValue from HL_Dictionary ");
			stringBuilder.Append(" where FieldID=@FieldID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FieldID", SqlDbType.Int, 4)
			};
			array[0].Value = FieldID;
			HKB.Data.Model.Dictionary dictionary = new HKB.Data.Model.Dictionary();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.Dictionary result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.Dictionary DataRowToModel(DataRow row)
		{
			HKB.Data.Model.Dictionary dictionary = new HKB.Data.Model.Dictionary();
			if (row != null)
			{
				if (row["FieldID"] != null && row["FieldID"].ToString() != "")
				{
					dictionary.FieldID = int.Parse(row["FieldID"].ToString());
				}
				if (row["Title"] != null)
				{
					dictionary.Title = row["Title"].ToString();
				}
				if (row["TableName"] != null)
				{
					dictionary.TableName = row["TableName"].ToString();
				}
				if (row["FieldName"] != null)
				{
					dictionary.FieldName = row["FieldName"].ToString();
				}
				if (row["FieldText"] != null)
				{
					dictionary.FieldText = row["FieldText"].ToString();
				}
				if (row["FieldValue"] != null)
				{
					dictionary.FieldValue = row["FieldValue"].ToString();
				}
			}
			return dictionary;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select FieldID,Title,TableName,FieldName,FieldText,FieldValue ");
			stringBuilder.Append(" FROM HL_Dictionary ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" FieldID,Title,TableName,FieldName,FieldText,FieldValue ");
			stringBuilder.Append(" FROM HL_Dictionary ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_Dictionary ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.FieldID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_Dictionary T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.Dictionary GetDictionary(string fieldValue)
		{
			HKB.Data.Model.Dictionary result = null;
			string text = "select * from HL_Dictionary where FieldValue='" + fieldValue + "'";
			DataSet dataSet = Conn.Query(text.ToString(), new IDbDataParameter[0]);
			if (dataSet != null)
			{
				if (dataSet.Tables[0].Rows.Count > 0)
				{
					result = new HKB.Data.Model.Dictionary();
					result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
				}
			}
			return result;
		}
	}
}
